<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title></title>
	<link rel="stylesheet" type="text/css" href="css/common.css"/>
	<link rel="stylesheet" type="text/css" href="css/article_list.css"/>
</head>
<body>
<form action="" method="post" class="search_box">
	<span>推广名称：<input type="text" id="name" name="name"/></span>

	<button type="button" class="sub">搜索</button>
</form>
<div id="list_content">

</div>

<div class="tcdPageCode"></div>
<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="layui/layui.js"></script>
<script src="js/jquery.page.js" type="text/javascript" charset="utf-8"></script>
<script>
			jQuery.browser = {};
			(function() {
				jQuery.browser.msie = false;
				jQuery.browser.version = 0;
				if(navigator.userAgent.match(/MSIE ([0-9]+)./)) {
					jQuery.browser.msie = true;
					jQuery.browser.version = RegExp.$1;
				}
			})();

			var params = {
				name:'', //姓名
				limit: 10, //记录数
				offset: 1 //页码
			};
			$(document).ready(function() {

				search();//搜索

					//绑定搜索事件
					$(".sub").click(function() {
						params.name = $("#name").val();
						search();
					});

			});
			//删除方法
			function del(ids) {
			  var id=ids.split('_')[0],status=ids.split('_')[1];
			  if(status=="0"){
			    status="1";
			  }else{
			    status="0";
			  }

				$.ajax({
					url: '/type/edit' ,
					type: 'POST',
					data: JSON.stringify({id:id,
								       status:status}),
					contentType : 'application/json;utf-8',
					success: function(data) {
						if(data.rel) {
							layui.use('layer', function() {
								var layer = layui.layer;
								layer.msg('设置成功', {
									icon: 6
								})
							})
							search();
						}
					}
				});
			}
			//查询方法
			function search() {

				$.ajax({
					url: '/type/list',
					type: 'get',
					data: params,
					dataType: "json",
					success: function(data) {
						var headTable = "<table border='0' cellspacing='0' cellpadding='0'><tr><th>ID</th><th style='text-align: left;'>推广名称</th>" +
							"<th>状态</th><th>操作</th></tr>";
						var contentHtml = "";
						if(data.rows) {
							var reg = new RegExp(",", "g");
							for(var i = 0; i < data.rows.length; i++) {
								var obj = data.rows[i];
								var index = i + 1;
								var statusHtml = "",statusButtonHtml="";
								if(obj.status==0) {
									statusHtml="启用",statusButtonHtml="<button id='" +  obj.id +"_"+obj.status + "' class='del'>禁用</button>";
								} else {
									statusHtml = "禁用",statusButtonHtml="<button id='" + obj.id +"_"+obj.status + "' class='del'>启用</button>";
								}
								contentHtml += "<tr><td>" + obj.id + "</td><td style='text-align: left;'>" + obj.name + "</td>" +
									"<td  >" + statusHtml + "</td>"+
									"<td>"+statusButtonHtml+"</td>	</tr>";
							}
						} else {
							contentHtml = "<tr><td colspan='5'>暂无记录</td></tr>";
						}

						$("#list_content").html(headTable + contentHtml + "</table>");
						//绑定删除事件
						$(".del").click(function() {

							var id = $(this).attr("id");
							var htmlInfo=$(this).html();
							layui.use('layer', function() {
								var layer = layui.layer;
								layer.confirm('确认'+htmlInfo, {icon: 3},function(index){
									layer.close(index)
									del(id);
								},function(index){
									layer.close(index)
								})
							})
							/*if(confirm("确认删除此信息？")){
									del($(this).attr("id"));
							}*/
						});
						$(".tcdPageCode").createPage({
							pageCount: data.totalPages,
							current: data.page,
							backFn: function(p) {
								//console.log(p);
								params.offset = p;
								search();
							}
						});
					}
				});
			}

			document.onkeydown = function(event){
				var e = event || window.event || arguments.callee.caller.arguments[0];
				if(e && e.keyCode == 116){
					event.preventDefault();

					$(window.parent.$('.showIframe').each(function(){
						if(!$(this).hasClass('hideIframe')){
							$(this).children('iframe').attr('src',$(this).children('iframe').attr('src'))
						}
					}))
				}
			}
		</script>
</body>
</html>
